package cn.itsource.org.service.impl;

import cn.itsource.basic.query.BasicQuery;
import cn.itsource.basic.service.impl.BasicServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
       // System.out.println("保存之前"+department);
        departmentMapper.save(department);// 保存的时候获取新增数据的主键--mybatis-day01
        //System.out.println("保存之后"+department);


        //给dirPath处理  -- /父级部门/父级部门/自己的id
        /*
            1.如果没有传上级部门  dirPath /自己的id
            2.如果传了上级部门
                11-->    /1/11/id
               查询id为11 数据   找到 dirPath + /自己的id
               1-->     /1/id
         */
        //没有选择上级部门的情况
        if (department.getParent()==null ||department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirpath
            Department dept = departmentMapper.loadById(department.getParent().getId());
            department.setDirPath(dept.getDirPath()+"/"+department.getId());
        }
        //更新数据
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //没有选择上级部门的情况
        if (department.getParent()==null ||department.getParent().getId()==null){
            department.setDirPath("/"+department.getId());
        }else {
            //获取上级部门的dirpath
            Department dept = departmentMapper.loadById(department.getParent().getId());
            department.setDirPath(dept.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    public void deleteByIds(Long[] ids) {
        departmentMapper.deleteByIds(ids);
    }

    @Override
    public List<Department> deptTree() {
        return departmentMapper.deptTree();
    }

    @Override
    public List<Department> deptTrees() {
        //查询一级和二级部门
        List<Department> departments = departmentMapper.deptTree();
        //遍历所有的一级部门
        for (Department department : departments) {
            //获取所有的二级部门
            List<Department> children = department.getChildren();
            //依次遍历查询三级和四级部门
            this.findTree(children);
        }
        return departments;
    }

    private void findTree(List<Department> departments) {
        if(departments.size()!=0){ //如果有二级部门
            //遍历二级部门
            for (Department department : departments) {
                //通过二级部门id查询三级部门
                List<Department> list =  departmentMapper.findParentId(department.getId());
                if(list.size()!=0){
                    //将查询到的三级部门放到二级部门的children中
                    department.setChildren(list);
                    //继续查询
                    this.findTree(list);
                }
            }
        }
    }
}
